package com.example.test_20210630;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileInputOutput {

    // 图片的copy只能用字节流
    public static void main(String[] args) throws Exception {
//        copyIn();
//        copyOut();
        File file = new File("C:/Users/leizi/Desktop/salary接口/颜值排行榜");
        dealFile(file);
    }

    private static void dealFile(File file)  throws Exception{
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for (File f:files) {
                dealFile(f);
            }
        }else{
            copyIn(file);
        }
    }

    private static void copyIn(File file)  throws Exception{
        // 读取硬盘中图片，写入项目指定目录下
        FileInputStream inputStream = new FileInputStream(file.getAbsolutePath());

        FileOutputStream outputStream = new FileOutputStream("src/" +
                "main/java/com/example/test_20210630/"+file.getName());
        byte[] bytes = new byte[1024];
        int len;
        while((len = inputStream.read(bytes)) != -1){
            outputStream.write(bytes,0,len);
        }
        inputStream.close();
        outputStream.close();
    }

    private static void copyOut()  throws Exception{
        // 读取项目中图片，写入硬盘指定目录下
        FileInputStream inputStream = new FileInputStream("src/main/java/com/example/test_20210630/test.jpg");

        FileOutputStream outputStream = new FileOutputStream("C:/Users/leizi/Desktop/美女1.jpg");
        byte[] bytes = new byte[1024];
        int len;
        while((len = inputStream.read(bytes)) != -1){
            outputStream.write(bytes,0,len);
        }
        inputStream.close();
        outputStream.close();
    }
}
